<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\Show;
use common\models\UserInfo;

/**
 * ShowSearch represents the model behind the search form about `backend\models\Show`.
 */
class ShowSearch extends Show
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
    	$user =UserInfo::findBySql("select username from user_info")->all();
    	$_arr = array();
		foreach ($user as $value)
		{
			array_push($_arr, $value['username']);
		}


        return [
            [['id',  'img_small_id', 'img_thumb_id', 'img_url_id', 'space_id'], 'integer'],
            [['name', 'content'], 'safe'],
            [['publish_time','top_time','state', 'app_return', 'publish_uid'],'string'],
            ['state','in','range'=>['美图','美文','大赛','原创设计'],'message'=>'请填入图片|美文|大赛|原创设计'],
            ['app_return','in','range'=>['返回','不返回'],'message'=>'请填入返回|不返回'],
            ['publish_uid','in','range' => $_arr,'message'=>'不存在该用户'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Show::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'state' => $this->setState($this->state),
            'app_return' => $this->setAppreturn($this->app_return),
            'img_small_id' => $this->img_small_id,
            'img_thumb_id' => $this->img_thumb_id,
            'img_url_id' => $this->img_url_id,
            'space_id' => $this->space_id,
            //'top_time' => $this->top_time,
            'publish_uid' => $this->setUsername($this->publish_uid),
           // 'publish_time' => $this->publish_time,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'content', $this->content])
         	->andFilterWhere(['like', 'FROM_UNIXTIME(publish_time, "%Y-%m-%d")', $this->publish_time])//按照天来查找
         	->andFilterWhere(['like', 'FROM_UNIXTIME(top_time, "%Y-%m-%d")', $this->setToptime($this->top_time)])
         ;

        return $dataProvider;
    }
    
    public function setStatus($string)
    {
    	$_arr = [
    	''=>'',
    	'编辑'=>'1',
    	'审核'=>'2',
    	'发送'=>'3'
    			];
    	return $_arr[$string];
    }
    public function setToptime($string)
    {
    	if($string=='不置顶'){return 0;}
    	else {
    		
    		return $string;
    	}   	
    }
    public function setUsername($string)
    {
    	$model = AdminInfo::getUIdByUsername($string);
    	if($model != null) return $model->id; 	
    }
    public function setState($string)
    {
    	
    	$_arr = array(''=>'','美图'=>'0','美文'=>'1','大赛'=>'2','原创设计'=>'3');
    	if(array_key_exists($string, $_arr)) 	
    	return $_arr[$string];
    	else return -1;
    }
    public function setAppreturn($string)
    {
    	$_arr = array(''=>'','返回'=>'1','不返回'=>'0');
    	if(array_key_exists($string, $_arr))
    	return $_arr[$string];
    	else return -1;
    }
}
